package org.jeecg.modules.friends.base;

import java.util.List;

import org.apache.commons.compress.utils.Lists;

import com.alibaba.fastjson.JSON;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.MD5;
import lombok.extern.slf4j.Slf4j;

/**
 * Created on 2022-12-31
 */
@Slf4j
public class JsonTool {
    public static <T> List<T> parseArray(String text, Class<T> clazz) {
        if (StrUtil.isEmpty(text)) {
            return Lists.newArrayList();
        }
        try {
            return JSON.parseArray(text, clazz);
        } catch (Exception e) {
            log.error("parseArray error.", e);
            NotifyTool.weChatAlarm(e);
            return Lists.newArrayList();
        }

    }

    public static <T> T parseObject(String text, Class<T> clazz) {
        if (StrUtil.isEmpty(text)) {
            try {
                return clazz.newInstance();
            } catch (Exception e) {
                log.error("e");
                try {
                    return clazz.newInstance();
                } catch (Exception ee) {
                    log.error("e", ee);
                }
            }
        }
        T t = JSON.parseObject(text, clazz);
        if (t != null) {
            return t;
        }
        try {
            return clazz.newInstance();
        } catch (Exception e) {
            log.error("e", e);
            return null;
        }
    }

    public static String toMD5(Object object) {
        if (object == null) {
            return MD5.create().digestHex("");
        }
        return MD5.create().digestHex(JSON.toJSONString(object));
    }
}
